
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('-')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return `${[year, month, day].map(formatNumber).join('-')}`
}
const formatDateTime_str = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  if(month.length==1){
    const month = '0' + month
  }
  const day = date.getDate()
  if(day.length==1){
    const day = '0' + day
  }
  const hour = date.getHours()
  const minute = date.getMinutes()
  return year + '年' + month +'月'+ day + '日 ' + hour + '时' + minute + '分'
}


// 获取距今日N天的日期
const daysLaterOrAfter=(date_str,days)=>{
  var the_date = new Date(date_str)
  the_date.setDate(the_date.getDate()+days);//获取days天后的日期
  var y = the_date.getFullYear();
  var m = the_date.getMonth()+1;//获取当前月份的日期
  if(m<10){
    m = '0' + m
  }
  var d = the_date.getDate();
  if(d<10){
    d = '0' + d
  }
  return y+"-"+m+"-"+d;
}
 


module.exports = {
  formatDateTime_str,
  formatTime,
  formatDate,
  daysLaterOrAfter,
}
